package com.gxa.community.customer.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.community.core.pojo.domain.Ad;
import com.gxa.community.core.pojo.domain.GoodsClass;
import com.gxa.community.core.pojo.domain.UserAgentApplication;
import com.gxa.community.core.pojo.dto.SeckillQueryDto;
import com.gxa.community.core.pojo.dto.content.TopicGoodsDto;
import com.gxa.community.core.pojo.dto.goods.GoodsInfoDto;
import com.gxa.community.core.pojo.vo.admin.user.UserSelfVo;
import com.gxa.community.core.utils.R;
import com.gxa.community.customer.config.RedisUtils;
import com.gxa.community.customer.service.HomeService;
import com.gxa.community.customer.util.JsonUtils;
import com.gxa.community.customer.util.RangeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Calendar;
import java.util.List;

@Slf4j
@Service
public class HomeServiceImpl implements HomeService {
    @Autowired
    RedisUtils redisUtils;
    private static final String AD = "ad"; //广告
    private static final String CLASS = "class"; //分类商品信息
    private static final String GOODSONESKILL = "goodsOneKill"; //当前最近的秒杀信息
    private static final String GOODSLISTKILL = "goodsListKill"; //当天所有的秒杀信息
    private static final String GROPUQUERLIST = "gropuQueryList"; //当天所有的拼团信息
    private static final String QUERYTOPLIST = "queryTopList"; //所有专题信息
    private static final String QUERYSELFLIST = "querySelfList"; //所有自提点信息

    private static final String URL = "http://localhost:8080";

    /**
     * 获取首页轮播图片
     * @return R
     */
    @Override
    public R queryAd() {
        Object o = redisUtils.get(AD);
        String Ads = null;
        if (StringUtils.isEmpty(o)){
            log.info("首页:Redis没有数据-->正在远程通信");
            String url = URL+"/content/querAd";
            String s = RangeUtils.get(url);
            if (s==null){
                log.info("首页:没有广告数据");
                return R.error("没有广告数据");
            }
            redisUtils.set(AD,s,60*60*2);
            Ads = s;
        }else {
            Ads = (String) o;
        }
        List<Ad> ads = JsonUtils.jsonToList(Ads, Ad.class);
        return R.ok().put("data",ads);
    }

    /**
     * 获取所有的分类
     * @return R
     */
    @Override
    public R queryClassList() {
        Object o = redisUtils.get(CLASS);
        String clazz = null;
        if (StringUtils.isEmpty(o)){
            log.info("首页:Redis没有数据-->正在远程通信");
            String url = URL+"/goods/class/queryClassList";
            String s = RangeUtils.get(url);
            if (s==null){
                log.info("首页:没有分类数据");
                return R.error("没有分类数据");
            }
            redisUtils.set(CLASS,s,60*60*2);
            clazz = s;
        }else {
            clazz = (String) o;
        }
        List<GoodsInfoDto> clazzz = JsonUtils.jsonToList(clazz, GoodsInfoDto.class);
        return R.ok().put("data",clazzz);
    }

    /**
     * 获取首页秒杀
     * @return R返回最近秒杀的时间
     */
    @Override
    public R queryKillGoodsOne() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        String goods = "";
        Object o = redisUtils.get(GOODSONESKILL);
        if (StringUtils.isEmpty(o)){
            log.info("首页:Rdies里没有首页秒杀信息");
            String url = URL+"/seckill/queryGoodsOne";
            goods = RangeUtils.get(url);
            if (goods==null){
                log.info("首页:数据库没有最新的秒杀信息");
                return R.error("数据库没有最新的秒杀信息");
            }
            Calendar calendar = Calendar.getInstance();
            int i = Math.abs(calendar.get(calendar.MINUTE) - 60);
            //获取当前时间和距离下个小时还有偶多少秒,Redis里就存多少秒
            redisUtils.set(GOODSONESKILL,goods,i*60);
        }else {
            goods = (String) o;
        }
        SeckillQueryDto seckillQueryDto = objectMapper.readValue(goods, SeckillQueryDto.class);
        return R.ok().put("data",seckillQueryDto);
    }
    /**
     * 获取所有的秒杀信息包含秒杀商品
     * @return R
     */
    @Override
    public R queryKillGoodsList() {
        String goods = "";
        Object o = redisUtils.get(GOODSLISTKILL);
        if (StringUtils.isEmpty(o)){
            log.info("首页:Rdies里没有列表秒杀信息");
            String url = URL+"/seckill/queryGoodsList";
            goods = RangeUtils.get(url);
            if (goods==null){
                log.info("首页:数据库没有秒杀信息");
                return R.error("数据库没有秒杀信息");
            }
            Calendar calendar = Calendar.getInstance();
            int i = Math.abs(calendar.get(calendar.MINUTE) - 60);;
            //获取当前时间和距离下个小时还有偶多少秒,Redis里就存多少秒
            redisUtils.set(GOODSLISTKILL,goods,i*60);
        }else {
            goods = (String) o;
        }
        List<SeckillQueryDto> seckillQueryDtoList = JsonUtils.jsonToList(goods, SeckillQueryDto.class);
        return R.ok().put("data",seckillQueryDtoList);
    }

    /**
     * 获取所有团购活动包含团购商品
     *
     * @return R
     */
    @Override
    public R queryGropuList() {
        String goods = "";
        Object o = redisUtils.get(GROPUQUERLIST);
        if (StringUtils.isEmpty(o)){
            log.info("首页:Rdies里没有团购活动信息");
            String url = URL+"/group/gropuQueryList";
            goods = RangeUtils.get(url);
            if (goods==null){
                log.info("首页:数据库没有团购活动信息");
                return R.error("数据库没有团购活动信息");
            }
            Calendar calendar = Calendar.getInstance();
            int i = Math.abs(calendar.get(calendar.MINUTE) - 60);
            //获取当前时间和距离下个小时还有偶多少秒,Redis里就存多少秒
            redisUtils.set(GROPUQUERLIST,goods,i*60);
        }else {
            goods = (String) o;
        }
        List<SeckillQueryDto> seckillQueryDtoList = JsonUtils.jsonToList(goods, SeckillQueryDto.class);
        return R.ok().put("data",seckillQueryDtoList);
    }

    /**
     * 获取所有的专题信息
     * @return R
     */
    @Override
    public R queryTopList() {
        String goods = "";
        Object o = redisUtils.get(QUERYTOPLIST);
        if (StringUtils.isEmpty(o)){
            log.info("首页:Redis里没有专题信息");
            String url = URL+"/content/queryTopList";
             goods = RangeUtils.get(url);
            if (goods==null){
                log.info("数据库没有专题信息");
                return R.error("数据库没有专题信息");
            }
            redisUtils.set(QUERYTOPLIST,goods,60*60*2);
        }else {
            goods = (String) o;
        }
        List<TopicGoodsDto> topicGoodsDtos = JsonUtils.jsonToList(goods, TopicGoodsDto.class);
        return R.ok().put("data",topicGoodsDtos);
    }

    /**
     * 获取所有自提点
     * @return R
     */
    @Override
    public R querySelfList() {
        Object o = redisUtils.get(QUERYSELFLIST);
        String self = null;
        if (StringUtils.isEmpty(o)){
            String url = URL+"/user/queryUsetAppList";
            self = RangeUtils.get(url);
            if (self==null){
                return R.error("数据库没有自提点信息");
            }
            redisUtils.set(QUERYSELFLIST,self,60*60*2);
        }else {
            self = (String) o;
        }
        List<UserSelfVo> json = JsonUtils.jsonToList(self, UserSelfVo.class);
        return R.ok().put("data",json);
    }

}
